//
//  AppSafari.swift
//  XiaoweHealthy
//
//  Created by Riven on 2022/7/29.
//

import Foundation
import SafariServices

struct AppSafari {
    
    static func gotoUserProtocol(at vc: UIViewController) {
        let userProtocolUrl = AppDelegate.appUserProtocolUrl
        present(at: vc, urlStr: userProtocolUrl)
    }
    
    static func gotoPrivacyProtocol(at vc: UIViewController) {
        let privacyProtocolUrl = AppDelegate.appPrivacyProtocolUrl
        present(at: vc, urlStr: privacyProtocolUrl)
    }
    
    static func present(at vc: UIViewController, urlStr: String) {
        var reqUrlStr = urlStr
        if !reqUrlStr.contains("http", caseSensitive: false) {
            reqUrlStr = "https://\(reqUrlStr)"
        }
        
        let cUrlStr = reqUrlStr.addingPercentEncoding(withAllowedCharacters: CharacterSet(charactersIn: "`%^{}\"[]|\\<> ").inverted)
        guard let url = cUrlStr?.url else {
            return
        }
        
        let sf = SFSafariViewController(url: url)
        sf.view.backgroundColor = .white
        sf.preferredBarTintColor = UIColor(hex: 0x000000, transparency: 0.9)
        sf.preferredControlTintColor = .white
        
        vc.present(sf, animated: true) { }
    }
    
}


// MARK: - Key of car (车钥匙)
extension AppSafari {
    
    static func gotoDownloadCarApp(at vc: UIViewController) {
        let carAppUrl = "https://tianmei.coolwellcloud.com/tianmei/download/"
        present(at: vc, urlStr: carAppUrl)
    }
    
    static func gotoCarUserProtocol(at vc: UIViewController) {
        let url = "https://files.xiaowe.cc/policy.html"
        present(at: vc, urlStr: url)
    }
    
    static func gotoCarPrivacyProtocol(at vc: UIViewController) {
        let url = "https://files.xiaowe.cc/privacy.html"
        present(at: vc, urlStr: url)
    }
    
}

